package com.cy.util;

import com.alibaba.fastjson.JSON;
import com.cy.pojo.*;
import com.cy.service.MyRedisService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2017/8/23.
 */
public class ZJUtil {
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmm");
    public static final ConcurrentHashMap<String, PushRecordPojo> pushRecordMap = new ConcurrentHashMap();

    /**
     * 计算所有交易所一天的所有数据
     */
    public static void minHandle(MongodbUtil mongodbUtil, MyRedisService myRedisService){
        Map<String, Object> query = new HashMap<>();
        RedisPojo rp = myRedisService.get(MongoNamePojo.LASTTID);
        Map<String, String> map = JSON.parseObject(rp.getNickname(), HashMap.class);
        Map<String, Object> eMap = new HashMap<>();
        for (Map.Entry<String, String> exMap : map.entrySet()){
            String ex = exMap.getKey();
            query.put("ex", ex);
            try {
                query.put("nowTime", getBeforeTime(60l));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Map<String, String> coinMap = JSON.parseObject(exMap.getValue(), HashMap.class);
            //所有交易所的所有虚拟币的数据
            Map<String, Object> cMap = new HashMap<>();
            for (Map.Entry<String, String> coin : coinMap.entrySet()){
                query.put("coin", coin.getKey());
                List<MongoZJMinPojo> list = mongodbUtil.getAfterTimeData(query, MongoZJMinPojo.class, MongoNamePojo.MIN);
                List<MongoZJDayPojo> day = mongodbUtil.getData(getTime(query), MongoZJDayPojo.class, MongoNamePojo.DAY);
                Collections.sort(list, new TimeComparator());
                Map<String, List<MongoZJMinPojo>> resMin = splitListMin(list);
                Map<String, List<MongoZJDayPojo>> resDay = splitListDay(day);
                //取1分钟，5分钟，10分钟，15分钟，30分钟,1小时的数据
                MoneyFlow buy = getMoneyFlow(resMin.get("buy"), resDay.get("buy"));
                MoneyFlow sell = getMoneyFlow(resMin.get("sell"), resDay.get("sell"));
                Map<String, Object> data = new HashMap<>();
                data.put("buy", buy);
                data.put("sell", sell);
                cMap.put(coin.getKey(), data);
            }
            eMap.put(ex, cMap);
        }
        RedisPojo redisPojo = new RedisPojo();
        redisPojo.setNickname(JSON.toJSONString(eMap));
        redisPojo.setId(BasicPojo.FundFlow);
        myRedisService.add(redisPojo);
    }

    /**
     * 获取时间条件天
     * @return
     */
    public static Map<String, Object> getTime(Map<String, Object> map){
        Calendar cd = Calendar.getInstance();
        map.put("year", cd.get(Calendar.YEAR));
        map.put("month", cd.get(Calendar.MONTH) + 1);
        map.put("day", cd.get(Calendar.DAY_OF_MONTH));
        return map;
    }

    /**
     * 获取时间条件小时
     * @return
     */
    public static Map<String, Object> getTimeHour(Map<String, Object> map){
        Calendar cd = Calendar.getInstance();
        map.put("year", cd.get(Calendar.YEAR));
        map.put("month", cd.get(Calendar.MONTH) + 1);
        map.put("day", cd.get(Calendar.DAY_OF_MONTH));
        map.put("hour", cd.get(Calendar.HOUR_OF_DAY));
        return map;
    }

    /**
     * 获取前一天的时间条件
     * @return
     */
    public static Map<String, Object> getbeforeDay(Map<String, Object> map){
        Calendar cd = Calendar.getInstance();
        cd.setTimeInMillis(new Date().getTime() - 361000l);
        map.put("year", cd.get(Calendar.YEAR));
        map.put("month", cd.get(Calendar.MONTH) + 1);
        map.put("day", cd.get(Calendar.DAY_OF_MONTH));
        return map;
    }

    /**
     * 获取N分钟前的毫秒值
     * @param time
     * @return
     * @throws ParseException
     */
    private static Long getBeforeTime(Long time) throws ParseException {
       return getNowTime(simpleDateFormat.format(new Date())) - time * 60 * 1000;
    }

    /**
     * 获取当前分钟的毫秒值
     * @return
     */
    public static Long getNowTime(String time) throws ParseException {
        return simpleDateFormat.parse(time).getTime();
    }

    /**
     * 将数据拆分成买和卖
     * @param list
     * @return
     */
    private static Map<String, List<MongoZJMinPojo>> splitListMin(List<MongoZJMinPojo> list){
        Map map = new HashMap<>();
        List<MongoZJMinPojo> buy = new ArrayList<>();
        List<MongoZJMinPojo> sell = new ArrayList<>();
        for (MongoZJMinPojo m : list){
            if ("sell".equals(m.getType())){
                sell.add(m);
            } else {
                buy.add(m);
            }
        }
        map.put("buy", buy);
        map.put("sell", sell);
        return map;
    }

    private static Map<String, List<MongoZJDayPojo>> splitListDay(List<MongoZJDayPojo> list){
        Map map = new HashMap<>();
        List<MongoZJDayPojo> buy = new ArrayList<>();
        List<MongoZJDayPojo> sell = new ArrayList<>();
        for (MongoZJDayPojo m : list){
            if ("sell".equals(m.getType())){
                sell.add(m);
            } else {
                buy.add(m);
            }
        }
        map.put("buy", buy);
        map.put("sell", sell);
        return map;
    }

    private static MoneyFlow getMoneyFlow(List<MongoZJMinPojo> list, List<MongoZJDayPojo> day){
        MoneyFlow mf = null;
        try {
            mf = getFlow(list, getNowTime(simpleDateFormat.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (day != null && !day.isEmpty()){
            Map mmm = new HashMap<>();
            MongoZJDayPojo d = day.get(0);
            mmm.put("sum", d.getSum());
            mmm.put("count", d.getCount());
            mf.setDay(JSON.toJSONString(mmm));
        }
        return mf;
    }


    /**
     * 获取N分钟的数据之和
     * @param list
     * @return
     */
    private static MoneyFlow getFlow(List<MongoZJMinPojo> list, Long t){
        MoneyFlow mf = new MoneyFlow();
        if (list == null || list.isEmpty())
            return mf;
        String sum = "0.0";
        String count = "0.0";
        HashMap<String, String> map = new HashMap();
        for (int i =0; i < list.size(); i++){
            MongoZJMinPojo m = list.get(i);
            sum = BigDecimalUtil.add(sum, m.getSum());
            count = BigDecimalUtil.add(count, m.getCount());
            Long time = m.getNowTime();
            if (t - time <= 60000l){
                map.put("sum", sum);
                map.put("count", count);
                mf.setYi(JSON.toJSONString(map));
                mf.setCoin(m.getCoin());
                continue;
            }
            if (t - time <= 60000l * 5){
                map.put("sum", sum);
                map.put("count", count);
                mf.setWu(JSON.toJSONString(map));
                continue;
            }
            if (t - time <= 60000l * 10){
                map.put("sum", sum);
                map.put("count", count);
                mf.setShi(JSON.toJSONString(map));
                continue;
            }
            if (t - time <= 60000l * 15){
                map.put("sum", sum);
                map.put("count", count);
                mf.setShiwu(JSON.toJSONString(map));
                continue;
            }
            if(t - time <= 60000l * 30){
                map.put("sum", sum);
                map.put("count", count);
                mf.setSanshi(JSON.toJSONString(map));
                continue;
            }
            map.put("sum", sum);
            map.put("count", count);
            mf.setHour(JSON.toJSONString(map));
        }
        return mf;
    }

}
